package com.yasha.ulti;


import java.util.regex.Pattern;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;
import android.util.Patterns;

public class Tools {

	public static int TYPE_WIFI = 1;
	public static int TYPE_MOBILE = 2;
	public static int TYPE_NOT_CONNECTED = 0;

	public static int getConnectivityStatus(Context context) {
		ConnectivityManager cm = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
		if (null != activeNetwork) {
			if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI)
				return TYPE_WIFI;
			if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE)
				return TYPE_MOBILE;
		}
		return TYPE_NOT_CONNECTED;
	}

	public static String getConnectivityStatusString(Context context) {
		int conn = getConnectivityStatus(context);
		String status = null;
		if (conn == TYPE_WIFI) {
			status = "Wifi enabled";
		} else if (conn == TYPE_MOBILE) {
			status = "Mobile data enabled";
		} else if (conn == TYPE_NOT_CONNECTED) {
			status = "Not connected to Internet";
		}
		return status;
	}
	
	public static String getUserEmail(Context context)
	{
		String email="";
		Pattern emailPattern = Patterns.EMAIL_ADDRESS; // API level 8+
		Account[] accounts = AccountManager.get(context).getAccounts();
		for (Account account : accounts) {
		    if (emailPattern.matcher(account.name).matches()) {
		    	email = account.name;
		    }
		}
		
		return email;
	}
	
	
	private static boolean debug=true;
	
	public static void Log(String title, String message) {
		if (debug == true)
			android.util.Log.i(title, message);
	}
	
	public static String sharePreferencesUserKey = "com.yasha.data.user";
	public static String UserEmailkey="email";
	public static String UserPhonekey="phone";
	public static String UserIDkey="id";
	
	public static String loadSavedPreferences(Context context,String code, String key) {
		SharedPreferences sharedPreferences = context.getSharedPreferences(
				code, Context.MODE_PRIVATE);
		String value = sharedPreferences.getString(key, "");
		return value;
	}

	public static boolean savePreferences(Context context ,String code, String key,
			String value) {
		boolean result = true;
		try {
			SharedPreferences sharedPreferences = context.getSharedPreferences(
					code, Context.MODE_PRIVATE);
			Editor editor = sharedPreferences.edit();
			editor.putString(key, value);
			editor.commit();
		} catch (Exception e) {
			result = false;
		}
		return result;

	}
}
